from draw_func import draw_pie
from draw_func import draw_bar
from draw_func import draw_line
from draw_func import count
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# 统一字符串数据


def upperH(x):
    if x == 'Very high':
        x = 'Very High'
    return x

# 转换为int类型


def toNumber(x):
    if type(x) == type('str'):
        x = x.replace(',', '')
        x = x.replace('.', '')
        x = int(x)
    elif type(x) == type(123):
        ...
    elif type(x) == type(1.0) and pd.notnull(x):
        x = int(x*1000)
    else:
        x = -1
    return x

# 按年份切片


def silceByYears(data):
    data2017 = data.loc[data['year'] == 2017]
    data2018 = data.loc[data['year'] == 2018]
    data2019 = data.loc[data['year'] == 2019]
    data2020 = data.loc[data['year'] == 2020]
    data2021 = data.loc[data['year'] == 2021]
    data2022 = data.loc[data['year'] == 2022]
    return data2017, data2018, data2019, data2020, data2021, data2022


data = pd.read_csv(r"QsRank.csv")

# 删去不要的列
data = data.drop(columns=['link', 'logo'])

# 处理格式不一的列
# data=data.fillna(-1)
data['faculty_count'] = data['faculty_count'].apply(toNumber)
data['research_output'] = data['research_output'].apply(upperH)
data['international_students'] = data['international_students'].apply(toNumber)

# 按年份分
data2017, data2018, data2019, data2020, data2021, data2022 = silceByYears(data)

draw_line(data, 'UCL', 'score', './qs/line')
draw_line(data, 'UCL', 'rank_display', './qs/line')
draw_pie(data2017, 'country', './qs/pie', '2017')
draw_pie(data2017, 'city', './qs/pie', '2017')
draw_pie(data2017, 'region', './qs/pie', '2017')
draw_pie(data, 'type', './qs/pie', 'all')
draw_pie(data, 'size', './qs/pie', 'all')
draw_bar(data2017, 'size', './qs/bar', '2017')
draw_bar(data2017, 'research_output', './qs/bar', '2017')
draw_pie(data2017, 'research_output', './qs/pie', '2017')
